﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;

using NUnit.Framework;

using Refix.Core.Repository;

namespace Refix.Core.Tests.Repository
{
    [TestFixture]
    public class VersionNumberComparerTest
    {
        [Test]
        public void Compare_SimpleNumberComparisonWorks()
        {
            var comparer = new VersionNumberComparer();
            int actual = comparer.Compare("1", "2");
            Assert.AreEqual(-1, actual, "Simple number comparison failed");
            actual = comparer.Compare("2", "2");
            Assert.AreEqual(0, actual, "Simple number comparison failed");
            actual = comparer.Compare("3", "2");
            Assert.AreEqual(1, actual, "Simple number comparison failed");
        }

        [Test]
        public void Compare_SecondPositionComparisonWorks()
        {
            var comparer = new VersionNumberComparer();
            int actual = comparer.Compare("2.1", "2.2");
            Assert.AreEqual(-1, actual, "Second position comparison failed");
            actual = comparer.Compare("2.2", "2.2");
            Assert.AreEqual(0, actual, "Second position comparison failed");
            actual = comparer.Compare("2.1", "1.2");
            Assert.AreEqual(1, actual, "Second position comparison was used when first should have been");
            actual = comparer.Compare("2.1.9.9.9", "2.2");
            Assert.AreEqual(-1, actual, "Second position comparison failed");
        }

        [Test]
        public void Compare_NonNumericWorks()
        {
            var comparer = new VersionNumberComparer();
            int actual = comparer.Compare("2.a", "2.b");
            Assert.AreEqual(-1, actual, "Non-numeric comparison failed");
            actual = comparer.Compare("2.f", "2.f");
            Assert.AreEqual(0, actual, "Non-numeric comparison failed");
            actual = comparer.Compare("c.b", "b.c");
            Assert.AreEqual(1, actual, "Second position comparison was used when first should have been");
            actual = comparer.Compare("2.a.9.9.9", "2.b");
            Assert.AreEqual(-1, actual, "Non-numeric position comparison failed");
        }
    }
}